<template>
	<view class="container container333638">
		<view v-if="data.code == 200" class="flex flex-wrap diygw-col-24 flex-direction-column">
			<image :src="data.data.img" class="response diygw-col-24" mode="widthFix"></image>
			<view class="flex flex-wrap diygw-col-24 items-center flex4-clz">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column flex35-clz">
					<text class="diygw-col-0 text22-clz">
						{{ data.data.title }}
					</text>
					<text class="diygw-text-line1 diygw-col-0">
						{{ data.data.remark }}
					</text>
				</view>
				<text class="diygw-text-line3 diygw-col-0 text2-clz"> ￥{{ data.data.price }} </text>
			</view>
			<view class="flex flex-wrap diygw-col-24 flex-direction-column justify-center items-center">
				<mp-html :content="data.data.content" class="diygw-col-24 ucontent-clz"></mp-html>
			</view>
		</view>
		<view class="flex flex-wrap diygw-col-24 justify-start diygw-bottom flex3-clz">
			<!-- #ifdef MP -->
			<button class="flex diygw-btn-default flex-wrap diygw-col-0 flex-direction-column justify-center items-center flex20-clz" open-type="contact">
				<text class="flex icon2 diygw-col-0 diy-icon-service"></text>
				<text class="diygw-col-0"> 客服 </text>
			</button>
			<!--  #endif -->
			<!-- #ifdef MP -->
			<button class="flex diygw-btn-default flex-wrap diygw-col-0 flex-direction-column justify-center items-center flex5-clz" open-type="share">
				<text class="flex icon3 diygw-col-0 diy-icon-forward"></text>
				<text class="diygw-col-0"> 分享 </text>
			</button>
			<!--  #endif -->
			<view class="flex flex-wrap diygw-col-0 flex-direction-column justify-center items-center flex2-clz" @tap="navigateTo" data-type="favorApi">
				<text class="flex icon1 diygw-col-0 diy-icon-favor"></text>
				<text class="diygw-col-0"> 收藏 </text>
			</view>
			<button class="flex diygw-btn-default flex-wrap diygw-col-0 justify-center items-center flex1-clz" @tap="navigateTo" data-type="phone" data-phone="15913132246">
				<text class="flex icon diygw-col-0 diy-icon-dianhua"></text>
				<text class="diygw-col-0"> 电话联系 </text>
			</button>
		</view>
		<view class="clearfix"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//用户全局信息
				userInfo: {},
				//页面传参
				globalOption: {},
				//自定义全局变量
				globalData: { isshow: false },
				dataNum: 1,
				data: {
					code: 0,
					msg: '',
					data: {
						id: 0,
						title: '',
						remark: '',
						img: '',
						content: null,
						cateId: 0,
						userId: 0,
						createTime: '',
						updateTime: '',
						deleteTime: null
					}
				},
				favor: {
					code: 200,
					msg: '新增成功',
					data: {
						userId: 1,
						id: 9
					}
				},
				productsNum: 1,
				products: {
					rows: [
						{
							id: 0,
							title: '',
							remark: '',
							img: '',
							price: '',
							content: '',
							productId: 0,
							cateId: null,
							touserId: 0,
							createTime: '',
							updateTime: '',
							deleteTime: null
						}
					],
					total: 0,
					code: 0,
					msg: ''
				}
			};
		},
		onShow() {
			this.setCurrentPage(this);
		},
		onLoad(option) {
			this.setCurrentPage(this);
			if (option) {
				this.setData({
					globalOption: this.getOption(option)
				});
			}

			this.init();
		},
		methods: {
			async init() {
				this.dataApi();
			},
			// 文章数据 API请求方法
			async dataApi(param) {
				let thiz = this;
				param = param || {};

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/cms/api.product/get';
				let http_data = {
					pageNum: this.dataNum,
					pageSize: 10,
					id: param.id || this.globalOption.id || '1'
				};
				let http_header = {};

				let data = await this.$http.post(http_url, http_data, http_header, 'json');

				if (!data.data) {
					this.showToast('详情不存在');
					uni.navigateBack();
					return;
				}

				this.data = data;
				this.globalData.isshow = true;
				uni.setNavigationBarTitle({
					title: data.data.title
				});
			},
			// 收藏接口 API请求方法
			async favorApi(param) {
				let thiz = this;
				param = param || {};

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/cms/productFavor/add';
				let http_data = {};
				let http_header = {};

				await this.productsApi({ productId: this.globalOption.id, userId: this.userInfo.userId });
				//如果已经收藏，不用再收藏
				if (this.products.total > 0) {
					this.showToast('你已收藏');
					return;
				}
				//把产品表的数据给了收藏表
				http_data = this.data.data;
				http_data.productId = this.globalOption.id;
				let fields = ['userId', 'id', 'createTime', 'updateTime', 'deleteTime'];
				fields.forEach((item) => {
					delete http_data[item];
				});
				http_data.userId = this.userInfo.userId;

				let favor = await this.$http.post(http_url, http_data, http_header, 'json');

				this.favor = favor;
				this.navigateTo({
					type: 'tip',
					tip: '收藏成功'
				});
			},
			// 判断是否收藏过 API请求方法
			async productsApi(param) {
				let thiz = this;
				param = param || {};

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/cms/productFavor/list';
				let http_data = {
					pageNum: this.productsNum,
					pageSize: 10,
					productId: param.productId || this.globalOption.id,
					isself: param.isself || '1'
				};
				let http_header = {};

				let products = await this.$http.post(http_url, http_data, http_header, 'json');

				this.products = products;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.flex4-clz {
		background-color: #ffffff;
		padding-top: 10rpx;
		flex: 1;
		padding-left: 10rpx;
		padding-bottom: 10rpx;
		padding-right: 10rpx;
	}
	.flex35-clz {
		flex: 1;
	}
	.text22-clz {
		font-weight: bold;
		font-size: 28rpx !important;
	}
	.text2-clz {
		padding-top: 10rpx;
		font-weight: bold;
		padding-left: 10rpx;
		font-size: 28rpx !important;
		padding-bottom: 10rpx;
		padding-right: 10rpx;
	}
	.ucontent-clz {
		padding-top: 10rpx;
		padding-left: 10rpx;
		padding-bottom: 10rpx;
		padding-right: 10rpx;
	}
	.flex3-clz {
		padding-top: 16rpx;
		border-bottom-left-radius: 0rpx;
		color: #747474;
		bottom: 0rpx;
		padding-left: 16rpx;
		padding-bottom: 16rpx;
		border-top-right-radius: 20rpx;
		background-color: #ffffff;
		overflow: hidden;
		left: 0rpx;
		border-top-left-radius: 20rpx;
		border-bottom-right-radius: 0rpx;
		padding-right: 16rpx;
	}
	.flex20-clz {
		margin-left: 10rpx;
		padding-top: 0rpx;
		padding-left: 10rpx;
		padding-bottom: 0rpx;
		margin-top: 0rpx;
		margin-bottom: 0rpx;
		margin-right: 10rpx;
		padding-right: 10rpx;
	}
	.icon2 {
		font-size: 40rpx;
	}
	.flex5-clz {
		margin-left: 10rpx;
		padding-top: 0rpx;
		padding-left: 10rpx;
		padding-bottom: 0rpx;
		margin-top: 0rpx;
		margin-bottom: 0rpx;
		margin-right: 10rpx;
		padding-right: 10rpx;
	}
	.icon3 {
		font-size: 40rpx;
	}
	.flex2-clz {
		margin-left: 10rpx;
		padding-top: 0rpx;
		padding-left: 10rpx;
		padding-bottom: 0rpx;
		margin-top: 0rpx;
		margin-bottom: 0rpx;
		margin-right: 10rpx;
		padding-right: 10rpx;
	}
	.icon1 {
		font-size: 40rpx;
	}
	.flex1-clz {
		padding-top: 16rpx;
		border-bottom-left-radius: 200rpx;
		color: #ffffff;
		padding-left: 10rpx;
		font-size: 28rpx !important;
		padding-bottom: 16rpx;
		border-top-right-radius: 200rpx;
		margin-right: 10rpx;
		margin-left: 10rpx;
		overflow: hidden;
		flex: 1;
		border-top-left-radius: 200rpx;
		margin-top: 0rpx;
		border-bottom-right-radius: 200rpx;
		background-image: linear-gradient(135deg, #2975ff 10%, #26b6ff 100%);
		margin-bottom: 0rpx;
		padding-right: 10rpx;
	}
	.icon {
		font-size: 40rpx;
	}
	.container333638 {
		background-image: url(/static/loginbg.png);
		background-position: top center;
		background-size: cover;
	}
</style>
